package com.gushen.library.utils;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.Environment;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;

import java.io.File;

public class FileUtils {

    public static String getOutputFilePath() {
        File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
        //公共路径需要一个单独的文件夹，否则报错！
        File tempFile = new File(directory.getAbsolutePath(), "/watermark");
        tempFile.mkdirs();
        File file = new File(tempFile.getAbsolutePath(), SystemClock.elapsedRealtime()+"waterMarkOutput.mp4");

        String outputPath = file.getAbsolutePath();
        return outputPath;
    }

    public static File getOutputFile() {
        File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
        //公共路径需要一个单独的文件夹，否则报错！
        File tempFile = new File(directory.getAbsolutePath(), "/watermark");
        tempFile.mkdirs();
        File file = new File(tempFile.getAbsolutePath(), SystemClock.elapsedRealtime()+"waterMarkOutput.mp4");
        return file;
    }

    public static Bitmap createWaterMark(View waterMarkView) {
        //判断当前系统是否为鸿蒙
        if (isHarmonyOS()){
            waterMarkView.setBackground(null);
        }

        //得到水印view的bitmap图片
        Bitmap bitmap = Bitmap.createBitmap(waterMarkView.getWidth(), waterMarkView.getHeight(), Bitmap.Config.ARGB_8888);
        waterMarkView.draw(new Canvas(bitmap));

        //这步是根据视频尺寸来调整图片宽高,和视频保持一致
        Matrix matrix =new Matrix();
        matrix.postScale(0.85f, 0.85f);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return bitmap;
    }


    public static boolean isHarmonyOS() {
        try {
            Class<?> buildExClass = Class.forName("com.huawei.system.BuildEx");
            Object osBrand = buildExClass.getMethod("getOsBrand").invoke(buildExClass);
            if (osBrand == null) {
                return false;
            }
            return "harmony".equalsIgnoreCase(osBrand.toString());
        } catch (Throwable e) {
            Log.i("SA.HasHarmonyOS", e.getMessage());
            return false;
        }
    }


    public static int dp2px(float dp) {
        float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int)(dp * scale + 0.5F);
    }
}
